package com.fwzc.giuseppe.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.adapter.HolderAdapter;
import com.fwzc.giuseppe.adapter.HolderAdapter.ViewCreator;
import com.fwzc.giuseppe.base.BaseActivity;

public class SelectAddress extends BaseActivity implements OnClickListener{
	private Context context = SelectAddress.this;
	private ListView lv;
	HolderAdapter<HashMap<String,String>> mAdapter;
	private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
	RelativeLayout add_address;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_address);
		initBar();
		actionbar_side_name.setText("选择收货地址");
		bg_actionbar_side.setBackgroundColor(getResources().getColor(R.color.transparent));
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		iv_actionbar_side_left.setOnClickListener(this);
		tv_actionbar_side_right.setVisibility(View.VISIBLE);
		tv_actionbar_side_right.setText("编辑");
		tv_actionbar_side_right.setOnClickListener(this);
		initView();
	}
	private void initView() {
		
		add_address = (RelativeLayout) findViewById(R.id.add_address);
		add_address.setOnClickListener(this);
		lv = (ListView) findViewById(R.id.address_listview);
		lv.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(); 
				intent.putExtra("name",data.get(arg2).get("name"));
				intent.putExtra("phone",data.get(arg2).get("phone"));
				intent.putExtra("address", data.get(arg2).get("address"));
				intent.putExtra("zipcode", data.get(arg2).get("zipcode"));
				intent.putExtra("addressId", data.get(arg2).get("addressId"));//地址唯一标识符
				intent.setClass(context, ConfirmationOrder.class);
				context.startActivity(intent);

			}
		});
		createAdapter();
		if (Api.false_data) {
			setFalseData();
		} else {

			loadingaddress();
			
		}
	}	
	private void loadingaddress() {
		// TODO Auto-generated method stub
		
	}
	private void createAdapter() {
		ViewCreator<HashMap<String, String>> creator = new ViewCreator<HashMap<String, String>>() {
			@SuppressLint("InflateParams")
			@Override
			public View createView(LayoutInflater inflater, int position,
					HashMap<String, String> map) {
				
				return LayoutInflater.from(context).inflate(
						R.layout.item_list_address, null);
			}  

			@Override
			public void updateView(View view, int position,
					final HashMap<String, String> map) {

				
				TextView name = (TextView) view.findViewById(R.id.tv_name);
				TextView phone = (TextView) view.findViewById(R.id.tv_phone);
				TextView address = (TextView) view.findViewById(R.id.tv_address);
				TextView zipcode = (TextView) view.findViewById(R.id.tv_zipcode);

				name.setText(map.get("name"));
				phone.setText(map.get("phone"));
				address.setText(map.get("address"));
				zipcode.setText(map.get("zipcode"));

			}
		};

		 mAdapter = new HolderAdapter<HashMap<String,String>>(this.getLayoutInflater(),creator);
		 lv.setAdapter(mAdapter);
		 mAdapter.update(data);
	}


	/**
	 * 伪数据
	 * */
		private void setFalseData() {
	        data.clear();
			
			HashMap<String,String> infomap = new HashMap<String,String>(); 
			infomap.put("name", "比利陈");
			infomap.put("phone", "13645765661");
			infomap.put("address", "西城新作101楼Z座");
			infomap.put("zipcode", "123456");
			data.add(infomap);
			
			HashMap<String,String> infomap1 = new HashMap<String,String>(); 
			infomap1.put("name", "比利川");
			infomap1.put("phone", "13645765661");
			infomap1.put("address", "西城新作101楼Z座");
			infomap1.put("zipcode", "123456");
			data.add(infomap1);
			
			HashMap<String,String> infomap2 = new HashMap<String,String>(); 
			infomap2.put("name", "艾比利");
			infomap2.put("phone", "13645765661");
			infomap2.put("address", "西城新作101楼Z座");
			infomap2.put("zipcode", "123456");
			data.add(infomap2);
			
			HashMap<String,String> infomap3 = new HashMap<String,String>(); 
			infomap3.put("name", "比利时");
			infomap3.put("phone", "12345678123");
			infomap3.put("address", "西城新作101楼Z座");
			infomap3.put("zipcode", "123456");
			data.add(infomap3);
		}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 返回
		case R.id.iv_actionbar_side_left:
			finish();
			break;
		case R.id.tv_actionbar_side_right:
			startActivity(new Intent(context, EditAddress.class));
			break;
		case R.id.add_address:
			startActivity(new Intent(context, AddAddress.class));
			break;
		default:
			break;
		}
		
	}

}
